import Vue from "vue";
import store from "@/store";
import DataDict from "@/utils/dict";
import list from "@/utils/dict/customDict.js";
import { getDicts as getDicts } from "@/api/system/dict/data";

function searchDictByKey(dict, key) {
  if (key == null && key == "") {
    return null;
  }
  try {
    for (let i = 0; i < dict.length; i++) {
      if (dict[i].key == key) {
        return dict[i].value;
      }
    }
  } catch (e) {
    return null;
  }
}

function install() {
  Vue.use(DataDict, {
    metas: {
      "*": {
        labelField: "dictLabel",
        valueField: "dictValue",
        request(dictMeta) {
          const storeDict = searchDictByKey(store.getters.dict, dictMeta.type);
          if (storeDict) {
            return new Promise((resolve) => {
              resolve(storeDict);
            });
          } else {
            return new Promise((resolve, reject) => {
              if (dictMeta.type == "sys_jump_id" || dictMeta.type =="sys_type_id") {
                store.dispatch("dict/setDict", {
                  key: dictMeta.type,
                  value: list[dictMeta.type],
                });
                resolve(list[dictMeta.type]);
              } else {
                getDicts(dictMeta.type)
                  .then((res) => {
                    store.dispatch("dict/setDict", {
                      key: dictMeta.type,
                      value: res.data,
                    });
                    resolve(res.data);
                  })
                  .catch((error) => {
                    reject(error);
                  });
              }
            });
          }
        },
      },
    },
  });
}

export default {
  install,
};
